﻿///<reference path="../../ExtJs/vswd-ext_2.2.js" />

var order_id;
var pageSize = 15;  // Grid每页显示个数
var cellWidth = 100; //单元格宽度

Ext.onReady(function() {
    Ext.QuickTips.init();
    // 本地加载图片代码
    Ext.BLANK_IMAGE_URL = strPath + 'extjs/resources/images/default/s.gif';

    pageSize = parseInt(((parent.Ext.get("ContentFrame").getHeight() - 108) - 1) / 21); //根据页面高度显示记录个数

    cellWidth = parseInt((parent.Ext.get("ContentFrame").getWidth() - 480 - 24));
    var httpProxy;

    // 设置获取订单列表的地址
    var httpProxyOrder_Main = new Ext.data.HttpProxy({
        url: strPath + 'GGFW/DocumentsManage/OrderCommand.fwf?Method=GetViewOrders'
    });

    var readerProxyOrder_Main = new Ext.data.JsonReader({
        root: 'Records',
        id: 'HWTransferItemID',
        totalProperty: 'totalCount',
        fields: [
            { name: 'order_id' }
          , { name: 'order_po' }
          , { name: 'order_date' }
          , { name: 'isdeleted' }
          , { name: 'client_id' }
          , { name: 'client_name' }
        ]
    });

    // 定义订单数据源
    Order_MainStore = new Ext.data.Store({
        proxy: httpProxyOrder_Main,
        reader: readerProxyOrder_Main,
        remoteSort: true
    });

    // 订单grid的选择列的事件
    var smOrderListModel = new Ext.grid.CheckboxSelectionModel({
        singleSelect: false,
        listeners: {
            rowselect: function(sm, row, rec) {
                order_id = rec.get("order_id");
            }
        }
    });

    // 设置显示列的Title
    var ChooseView = new Ext.grid.GridView({ forceFit: true, sortAscText: '升序', sortDescText: '降序' });
    ChooseView.columnsText = '列显示/隐藏';

    // 设置页脚的显示样式。
    var ChooseBar = new Ext.PagingToolbar({
        pageSize: pageSize,
        store: Order_MainStore,
        beforePageText: '第',
        afterPageText: '页 共{0}页',
        firstText: '第一页',
        prevText: '前一页',
        nextText: '后一页',
        lastText: '最后一页',
        refreshText: '刷新',
        displayInfo: true,
        displayMsg: '显示第 {0} - {1} 条数据, 共 {2} 条数据',
        emptyMsg: "没有数据可以显示"
    });

    // 污染源监测任务Grid显示内容
    var Order_MainlistColumnModel = new Ext.grid.ColumnModel([
        smOrderListModel,
        { id: 'order_id', header: "订单ID", align: 'center', sortable: false, dataIndex: 'order_id', hidden: true },
        { header: "订单号", align: 'center', sortable: true, dataIndex: 'order_po', width: 150, renderer: renderValue },
		{ header: "客户名", align: 'center', sortable: true, dataIndex: 'client_name', width: cellWidth, renderer: renderValue },
		{ header: "日期", align: 'center', sortable: true, dataIndex: 'order_date', renderer: renderDateValue, width: 150, hidden: false },
		{ header: "操作", align: 'center', sortable: false, dataIndex: 'order_id', width: 180, renderer: getitemlink }

     ]);

    /**
    *描述 : 渲染列，鼠标移该单元格上面，显示完整的值
    *参数 : value：单元格的值；
    */
    function renderValue(value, metadate, record, colIndex, rowIndex) {
        return '<div style="text-align:left;"><span title="' + value + '">' + value + '</span></div>';
    }

    // 删除按钮
    var buttonDelOrders = {
        id: 'btnDelOrders',
        text: '<font color="#000000">删除</font>',
        handler: function() {
            DelOrders();  // 删除调用的函数
        },
        iconCls: 'add',
        icon: strPath + 'images/ico_25.gif'

    };

    var labelSearchOrderPO = new Ext.form.Label({
        id: "lbSearchOrderPO",
        html: "<span>订单号(PO)：</span>"
    });

    var textfieldSearchOrderPO = new Ext.form.TextField({
        id: "tfSearchOrderPO",
        width: 128
    });

    var labelSearchClientName = new Ext.form.Label({
        id: "lbSearchClientName",
        html: "<span>&nbsp;&nbsp;客户名：</span>"
    });

    var textfieldSearchClientName = new Ext.form.TextField({
        id: "tfSearchClientName",
        width: 128
    });

    // 工具条
    var toolbarOrdersList = new Ext.Toolbar({
        height: 25,
        items: [buttonDelOrders, ' ', '-', ' ', labelSearchOrderPO, textfieldSearchOrderPO, labelSearchClientName, textfieldSearchClientName]
    });

    gridOrderList = new Ext.grid.EditorGridPanel({
        id: "Order_MainGrid",
        renderTo: "divOrder_Main",
        stripeRows: true,
        store: Order_MainStore,
        cm: Order_MainlistColumnModel,
        sm: smOrderListModel,
        width: parent.Ext.get("ContentFrame").getWidth(),
        height: parent.Ext.get("ContentFrame").getHeight() - 25,
        loadMask: { msg: '正在加载数据，请稍侯... ...' },
        frame: false,
        border: false,
        animCollapse: false,
        trackMouseOver: true,
        bbar: ChooseBar,
        tbar: toolbarOrdersList
    });

    //数据源开始读取数据
    doSearchOrdersList(true);
})

// 监测任务重新加载数据(刷新)
function doSearchOrdersList(isFirstPage) {
    if (Order_MainStore == null) return;
    // 定义查询参数
    //var TaskName = Ext.get("txtSearchTaskName").dom.value;  // 批次任务名称
    //var SamplingTime = Ext.get("txtSearchStartTime").dom.value;  // 采样时间
    // 传递查询参数并刷新
    if (isFirstPage) Order_MainStore.load({
        params: {
            start: 0, // 起始页
            limit: pageSize, // 每页显示个数
            order_po: Ext.get("tfSearchOrderPO").dom.value,
            client_name: Ext.get("tfSearchClientName").dom.value
        }
    });
    else Order_MainStore.load({
        params: {
            start: (gridOrderList.getBottomToolbar().getPageData().activePage - 1) * pageSize,  // 起始页
            limit: pageSize,  // 每页显示个数
            order_po: Ext.get("tfSearchOrderPO").dom.value,
            client_name: Ext.get("tfSearchClientName").dom.value
        }
    });
}

// 删除方法
function DelOrders() {
    var order_ids = getSelectID(gridOrderList, 'order_id');
    if (order_ids == '' || order_ids == null) {
        Ext.MessageBox.alert("提示", "请选择要删除的数据！"); return;
    }
    Ext.MessageBox.confirm("询问", '确认要删除选中的数据吗？', function(btn) {
        if (btn == "yes") {
            var strUrl = strPath + 'GGFW/DocumentsManage/OrderCommand.fwf?Method=DelOrdersByID';
            Ext.Ajax.request({
                url: strUrl,
                method: 'POST',
                params: {
                    order_ids: order_ids
                },
                failure: function(response, options) {
                    ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                },
                success: function(response, options) {
                    var json = response.responseText;
                    var obj = parseJsonResult(json);
                    if (!obj.Success) {
                        showMessage("错误！", "删除数据出现错误！请查看详细原因。", obj.Message);
                    }
                    else {
                        // 删除成功后刷新页面
                        doSearchOrdersList(false);
                    }
                }
            });
        }
    });
}

//页面跳转
function getitemlink(value, metadate, record, colIndex, rowIndex) {
    return "<a class='a_link' href=\"OrdersEdit.aspx?order_id=" + record.get("order_id") + "\">编辑</a> | <a class='a_link' href='#' onclick='PrintOrder(" + record.get("order_id") + ");'>打印订单</a> | <a class='a_link' href='#' onclick='PrintMO(" + record.get("order_id") + ");'>打印小单</a>";
}